﻿#include "FITKSEAJunctionArea.h"
#include "FITKAcousticsDiscModel.h"
#include "FITKAcousticsSEAModel.h"
#include "FITKAbstractSEASubSys.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"

namespace Acoustics
{
    FITKSEAJunctionArea::FITKSEAJunctionArea()
    {

    }

    FITKSEAJunctionArea::~FITKSEAJunctionArea()
    {

    }

    FITKAbstractJunction::JunctionType FITKSEAJunctionArea::getJunctiontType()
    {
        return FITKAbstractJunction::JunctionType::SEAAreaJunction;
    }

    bool FITKSEAJunctionArea::getIsUseResonant()
    {
        return _isUseResonant;
    }

    void FITKSEAJunctionArea::setIsUseResonant(bool isUse)
    {
        _isUseResonant = isUse;
    }

    bool FITKSEAJunctionArea::getIsUseNonresonant()
    {
        return _isUseNonresonant;
    }

    void FITKSEAJunctionArea::setIsUseNonresonant(bool isUse)
    {
        _isUseNonresonant = isUse;
    }

    bool FITKSEAJunctionArea::getIsUseLeak()
    {
        return _isUseLeak;
    }

    void FITKSEAJunctionArea::setIsUseLeak(bool isUse)
    {
        _isUseLeak = isUse;
    }

    bool FITKSEAJunctionArea::getIsUseTransmissionLoss()
    {
        return _isUseTransmissionLoss;
    }

    void FITKSEAJunctionArea::setIsUseTransmissionLoss(bool isUse)
    {
        _isUseTransmissionLoss = isUse;
    }

    void FITKSEAJunctionArea::setSpectrum(int id)
    {
        _spectrumID = id;
    }

    int FITKSEAJunctionArea::getSpectrum()
    {
        return _spectrumID;
    }

    void FITKSEAJunctionArea::setDirection(JunctionAreaDirection direction)
    {
        _direction = direction;
    }

    FITKSEAJunctionArea::JunctionAreaDirection FITKSEAJunctionArea::getDirection()
    {
        return _direction;
    }

    double FITKSEAJunctionArea::getAcousticCouplingAreaX()
    {
        return _acousticCouplingAreaX;
    }

    void FITKSEAJunctionArea::setAcousticCouplingAreaX(double value)
    {
        _acousticCouplingAreaX = value;
    }

    double FITKSEAJunctionArea::getAcousticRadiationAreaX()
    {
        return _acousticRadiationAreaX;
    }

    void FITKSEAJunctionArea::setAcousticRadiationAreaX(double value)
    {
        _acousticRadiationAreaX = value;
    }

    double FITKSEAJunctionArea::getAcousticRadiationPerimaterX()
    {
        return _acousticRadiationPerimaterX;
    }

    void FITKSEAJunctionArea::setAcousticRadiationPerimaterX(double value)
    {
        _acousticRadiationPerimaterX = value;
    }

    void FITKSEAJunctionArea::appendConnection(FITKSEAJunAreaConStr & connection)
    {
        _connections.append(connection);
    }

    int FITKSEAJunctionArea::getConnectionShellID()
    {
        FITKAcousticsDiscModel* discModel = FITKAPP->getGlobalData()->getMeshData<FITKAcousticsDiscModel>();
        if (discModel == nullptr)return -1;
        auto SEAModel = discModel->getSEAModel();
        if (SEAModel == nullptr)return -1;
        FITKAcousticsSEASubSysManager* subSysManager = SEAModel->getSEASubSysManager();
        if (subSysManager == nullptr)return -1;
        for (FITKSEAJunAreaConStr surStr : _connections) {
            FITKAbstractSEASubSys* subSys = subSysManager->getDataByID(surStr._subSysID);
            if (subSys == nullptr)continue;
            auto type = subSys->getSEASubSysType();
            switch (type){
            case Acoustics::FITKAbstractSEASubSys::SEAFlatPlate:
            case Acoustics::FITKAbstractSEASubSys::SEASinglyCurvedShell:
            case Acoustics::FITKAbstractSEASubSys::SEADoublyCurvedShell:
            case Acoustics::FITKAbstractSEASubSys::SEACylinderShell: {
                return subSys->getDataObjectID();
            }
            }
        }
        return -1;
    }

    void FITKSEAJunctionArea::replaceConnections(QList<FITKSEAJunAreaConStr>& connections)
    {
        _connections = connections;
    }

    QList<FITKSEAJunAreaConStr> FITKSEAJunctionArea::getConnections()
    {
        return _connections;
    }

    void FITKSEAJunctionArea::appendLeak(FITKSEAJunAreaLeakStr & leak)
    {
        _leaks.append(leak);
    }

    void FITKSEAJunctionArea::replaceLeaks(QList<FITKSEAJunAreaLeakStr>& leaks)
    {
        _leaks = leaks;
    }

    QList<FITKSEAJunAreaLeakStr> FITKSEAJunctionArea::getLeaks()
    {
        return _leaks;
    }

    void FITKSEAJunctionArea::updateConnect()
    {
        _connections.clear();
        for (int subSysID : _subSysIDList) {
            FITKSEAJunAreaConStr str;
            str._subSysID = subSysID;
            _connections.append(str);
        }

        this->FITKSEAAbstractJunction::initUserDefineMatrix(_subSysIDList);
    }

    void FITKSEAJunAreaLeakStr::calculateArea()
    {
        switch (_type)
        {
        case Acoustics::FITKSEAJunAreaLeakStr::Rectangular: 
        case Acoustics::FITKSEAJunAreaLeakStr::Slit: {
            _area = _length * _width;
            break;
        }
        case Acoustics::FITKSEAJunAreaLeakStr::Circular: {
            double pi = 3.1415926;
            _area = pi * _radius * _radius;
            break;
        }
        default:break;
        }
    }
}